home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / io.c < prev    next >
C/C++ Source or Header  |  1990-04-25  |  5KB  |  177 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* io.c */
  25.  
  26. #include "sim.h"
  27. #include "aux.h"
  28.  
  29. extern double floatval();
  30.  
  31. /* write_tnum() writes a tagged number out to stream F */
  32.  
  33. write_tnum(N, F)
  34. LONG N;
  35. FILE *F;
  36. {
  37.     double fv;
  38.  
  39.     switch((int)(N & 0x7)) {
  40.     case INT_TAG:
  41.         fprintf(F, "%d", INTVAL(N));
  42.         break;
  43.     case FLOAT_TAG:
  44.         fv = floatval(N);
  45.         fprintf(F, "%g", fv);
  46.         if ((int)fv == fv) fprintf(F, ".0");
  47.         break;
  48.     default: fprintf(stderr, "illegal argument to write_tnum: %x\n", N);
  49.     }
  50. }
  51.  
  52. writepname(file, name_ptr, length)
  53. FILE     *file;
  54. CHAR_PTR name_ptr;
  55. WORD     length;
  56. {
  57.    WORD i, slen;
  58.    CHAR ch;
  59.  
  60.    for (i = 1; i <= length; ++i) {
  61.     ch = *name_ptr++;
  62.     putc(((ch < ' ' && ch != '\n' && ch != '\t') ? ' ' : ch), file);
  63.     /* nonprintables to blanks */
  64.    }
  65.    /* fflush(file); */
  66. }
  67.  
  68. writeqname(file, name_ptr, length)
  69. FILE     *file;
  70. CHAR_PTR name_ptr;
  71. WORD     length;
  72. {
  73.    WORD i, need_to_quote;
  74.    CHAR ch;
  75.  
  76.    ch = *name_ptr;
  77.    if (ch >= 'a' && ch <= 'z')
  78.       need_to_quote = 0;
  79.    else need_to_quote = 1;
  80.    if (!need_to_quote) {
  81.       i = 1;
  82.       while (i < length) {
  83.      ch = *(name_ptr+i);
  84.      if (!((ch >= '0' && ch <= '9') ||
  85.            (ch >= 'A' && ch <= 'Z') ||
  86.            (ch == '_') ||
  87.            (ch >= 'a' && ch <= 'z'))) {
  88.             need_to_quote = 1;    /* quote if name contains spl chars */
  89.             break;
  90.      }
  91.      else i++;
  92.       }
  93.    }
  94.    if (need_to_quote)
  95.       fprintf(file, "'");
  96.    for (i = 1; i <= length; ++i) {
  97.       if (need_to_quote && *name_ptr == '\'')
  98.      fprintf(file, "'");
  99.        /* putc(*name_ptr++, file); */
  100.        putc(((*name_ptr < ' ' && *name_ptr != '\n' && *name_ptr != '\t') ?
  101.              ' ' : *name_ptr), file);
  102.        name_ptr++;
  103.    }
  104.    if (need_to_quote)
  105.       fprintf(file, "'");
  106.    /* fflush(file); */
  107. }
  108.  
  109. printterm(term, car)
  110. LONG term;
  111. BYTE car;
  112. {
  113.    WORD              i, arity;
  114.    PSC_REC_PTR       psc_ptr;
  115.    register LONG_PTR top;
  116.  
  117. ptd: 
  118.    switch (TAG(term)) {
  119.       case FREE:
  120.      NDEREF(term, ptd);
  121.      printf("_%x", UNTAGGED(term));
  122.      return;
  123.       case CS:
  124.          psc_ptr = GET_STR_PSC(term);
  125.      if (IS_BUFF(psc_ptr))               /* buffer */
  126.             printf("Buffer_%x", GET_NAME(psc_ptr));
  127.      else writepname(stdout, GET_NAME(psc_ptr), GET_LENGTH(psc_ptr));
  128.      arity = GET_ARITY(psc_ptr);
  129.      if (arity == 0)                     /* constant */
  130.         return;
  131.                                          /* structure */
  132.      printf("(");
  133.      UNTAG(term);
  134.      for (i = 1; i <= arity; i++) {
  135.             printterm(term += 4, CAR);
  136.             if (i < arity)                   /* not at end of struct */
  137.            printf(",");
  138.      }
  139.      printf(")");
  140.          /* fflush(stdout); */
  141.          return;
  142.       case NUM:
  143.      write_tnum(term, stdout);
  144.      return;
  145.       case LIST:
  146.      UNTAG(term);
  147.      if (car)
  148.         printf("[");
  149.      printterm(FOLLOW(term), CAR);
  150.      term = FOLLOW(term + 4);
  151. ldp:     switch (TAG(term)) {
  152.             case FREE:
  153.            NDEREF(term, ldp);
  154.            break;
  155.             case LIST:
  156.            printf(",");
  157.            printterm(term, CDR);
  158.            return;
  159.             case CS:
  160.            if (ISNIL(term)) {    /* term is the 'nil' constant */
  161.               printf("]");
  162.                   /* fflush(stdout); */
  163.                   return;
  164.            }
  165.            break;
  166.         case NUM:
  167.            break;
  168.      }
  169.      /* vertical bar case */
  170.          printf("|");
  171.      printterm(term, CAR);
  172.      printf("]");
  173.      /* fflush(stdout); */
  174.      return;
  175.    }
  176. }
  177.